/*
 * Copyright (c) 2025 彭友聪
 * nativePC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2. 
 * You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
 * See the Mulan PSL v2 for more details.  
 * 
 * Author: 彭友聪 
 * email：2923616405@qq.com 
 * date: 2025/10/8 08:42
 * file: ${FILE_NAME}
 * product: DevEco Studio
 * LICENSE: MulanPSL-2.0
 * */
//
// Created on 2025/10/8.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef NATIVEPC_NATIVEMODULE_H
#define NATIVEPC_NATIVEMODULE_H

#include <arkui/native_node.h>
#include <arkui/native_animate.h>
#include <arkui/native_node_napi.h>
#include <cassert>
#include "napi/native_api.h"
#include <string>
#include <arkui/native_interface.h>
#include <string>
#include <hilog/log.h>

extern napi_env g_env;
struct DynamicParams
{
        std::string desc;
        std::string *value;
};

inline DynamicParams *globalParams;
// 全局保存ArkTS回调的napi_ref
inline napi_ref g_clickCallbackRef = nullptr;
// 全局保存的 UIContext
inline ArkUI_ContextHandle globalUIContext = nullptr;
namespace NativeModule
{
    const unsigned int LOG_PRINT_DOMAIN = 0xFF00;
    static napi_value SaveUIContext(napi_env env, napi_callback_info info)
    {
        size_t argc = 1;
        napi_value args;
        napi_get_cb_info(env, info, &argc, &args, nullptr, nullptr);

        // 转换ArkTS参数为Native句柄
        OH_ArkUI_GetContextFromNapiValue(env, args, &globalUIContext);
            if (globalUIContext != nullptr) {
                OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "eventInfo", "获取UIContext成功");
            }
            else {
                OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "eventInfo", "获取UIContext失败");
            }
        return nullptr;
    }
    static napi_value NativeInvokeUpdateEventInfo(napi_env env, napi_callback_info info)
    {
        size_t argc = 1;
        napi_value args[1] = {nullptr};
        napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
        // 保存ArkTS回调引用
        napi_create_reference(env, args[0], 1, &g_clickCallbackRef);

            if (globalParams && globalParams->value) {
                napi_value argv = nullptr;
                std::string data = globalParams->desc + ": " + *globalParams->value;
//        std::string data = "测试";
                napi_status status = napi_create_string_utf8(env, data.c_str(), data.length(), &argv);
                    if (status != napi_ok) {
                        return nullptr;
                    }
                napi_value result = nullptr;
                status = napi_call_function(env, nullptr, args[0], 1, &argv, &result);
                    if (status != napi_ok) {
                        return nullptr;
                    }
                return result;
            }
            else {
                // 处理空指针异常或默认值情况
                napi_value argv = nullptr;
                std::string defaultStr = "default";
                napi_status status = napi_create_string_utf8(env, defaultStr.c_str(), defaultStr.length(), &argv);
                    if (status != napi_ok) {
                        return nullptr;
                    }
                napi_value result = nullptr;
                status = napi_call_function(env, nullptr, args[0], 1, &argv, &result);
                    if (status != napi_ok) {
                        return nullptr;
                    }
                return result;
            }
    }
    class NativeModuleInstance
    {
        public:
            static NativeModuleInstance *GetInstance()
            {
                static NativeModuleInstance instance;
                return &instance;
            }

            NativeModuleInstance()
            {
                // 获取NDK接口的函数指针结构体对象，用于后续操作。
                OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, arkUINativeNodeApi_);
                assert(arkUINativeNodeApi_);
                OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_ANIMATE, ArkUI_NativeAnimateAPI_1, animateApi_);
                assert(animateApi_);
            }
            // 暴露给其他模块使用。
            ArkUI_NativeNodeAPI_1 *GetNativeNodeAPI() { return arkUINativeNodeApi_; }
            ArkUI_NativeAnimateAPI_1 *GetNativeAnimateAPI() { return animateApi_; }

        private:
            ArkUI_NativeNodeAPI_1 *arkUINativeNodeApi_ = nullptr;
            ArkUI_NativeAnimateAPI_1 *animateApi_ = nullptr;
    };

} // namespace NativeModule

#endif // NATIVEPC_NATIVEMODULE_H
